/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package warmachinesystem.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

import warmachinesystem.ArmyList;
import warmachinesystem.BaseUnit;
import warmachinesystem.GameSystem;
import warmachinesystem.Solo;
import warmachinesystem.Unit;
import warmachinesystem.UnitAttachment;
import warmachinesystem.Warcaster;
import warmachinesystem.Warjack;
import warmachinesystem.WarmachineSystemPackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Army List</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link warmachinesystem.impl.ArmyListImpl#getBaseUnits <em>Base Units</em>}</li>
 *   <li>{@link warmachinesystem.impl.ArmyListImpl#getGameSystem <em>Game System</em>}</li>
 *   <li>{@link warmachinesystem.impl.ArmyListImpl#getName <em>Name</em>}</li>
 *   <li>{@link warmachinesystem.impl.ArmyListImpl#getCode <em>Code</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ArmyListImpl extends EObjectImpl implements ArmyList {
	/**
	 * The cached value of the '{@link #getBaseUnits() <em>Base Units</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBaseUnits()
	 * @generated
	 * @ordered
	 */
	protected EList<BaseUnit> baseUnits;

	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getCode() <em>Code</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCode()
	 * @generated
	 * @ordered
	 */
	protected static final String CODE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getCode() <em>Code</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCode()
	 * @generated
	 * @ordered
	 */
	protected String code = CODE_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ArmyListImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WarmachineSystemPackage.Literals.ARMY_LIST;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<BaseUnit> getBaseUnits() {
		if (baseUnits == null) {
			baseUnits = new EObjectContainmentWithInverseEList<BaseUnit>(BaseUnit.class, this, WarmachineSystemPackage.ARMY_LIST__BASE_UNITS, WarmachineSystemPackage.BASE_UNIT__ARMY_LIST);
		}
		return baseUnits;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Unit> getUnits() {
		EList<Unit> result = new BasicEList<Unit>();
		for (BaseUnit unit: baseUnits){
			if (unit instanceof Unit){
				result.add((Unit) unit);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Solo> getSolos() {
		EList<Solo> result = new BasicEList<Solo>();
		for (BaseUnit unit: baseUnits){
			if (unit instanceof Solo){
				result.add((Solo) unit);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<UnitAttachment> getUnitAttachments() {
		EList<UnitAttachment> result = new BasicEList<UnitAttachment>();
		for (BaseUnit unit: baseUnits){
			if (unit instanceof UnitAttachment){
				result.add((UnitAttachment) unit);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<UnitAttachment> getUnitAttachments(BaseUnit unit) {
		EList<UnitAttachment> result = new BasicEList<UnitAttachment>();
		for (BaseUnit baseUnit: baseUnits){
			if (baseUnit instanceof UnitAttachment){
				UnitAttachment ua = (UnitAttachment) baseUnit;
				if (ua.getAttachableUnits().contains(unit))
					result.add(ua);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public GameSystem getGameSystem() {
		if (eContainerFeatureID() != WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM) return null;
		return (GameSystem)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetGameSystem(GameSystem newGameSystem, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newGameSystem, WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setGameSystem(GameSystem newGameSystem) {
		if (newGameSystem != eInternalContainer() || (eContainerFeatureID() != WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM && newGameSystem != null)) {
			if (EcoreUtil.isAncestor(this, newGameSystem))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newGameSystem != null)
				msgs = ((InternalEObject)newGameSystem).eInverseAdd(this, WarmachineSystemPackage.GAME_SYSTEM__ARMIES, GameSystem.class, msgs);
			msgs = basicSetGameSystem(newGameSystem, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM, newGameSystem, newGameSystem));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WarmachineSystemPackage.ARMY_LIST__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getCode() {
		return code;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setCode(String newCode) {
		String oldCode = code;
		code = newCode;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WarmachineSystemPackage.ARMY_LIST__CODE, oldCode, code));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Warcaster> getWarcasters() {
		EList<Warcaster> result = new BasicEList<Warcaster>();
		for (BaseUnit unit: baseUnits){
			if (unit instanceof Warcaster){
				result.add((Warcaster) unit);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Warjack> getWarjacks() {
		EList<Warjack> result = new BasicEList<Warjack>();
		for (BaseUnit unit: baseUnits){
			if (unit instanceof Warjack){
				result.add((Warjack) unit);
			}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getBaseUnits()).basicAdd(otherEnd, msgs);
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetGameSystem((GameSystem)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				return ((InternalEList<?>)getBaseUnits()).basicRemove(otherEnd, msgs);
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				return basicSetGameSystem(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				return eInternalContainer().eInverseRemove(this, WarmachineSystemPackage.GAME_SYSTEM__ARMIES, GameSystem.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				return getBaseUnits();
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				return getGameSystem();
			case WarmachineSystemPackage.ARMY_LIST__NAME:
				return getName();
			case WarmachineSystemPackage.ARMY_LIST__CODE:
				return getCode();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				getBaseUnits().clear();
				getBaseUnits().addAll((Collection<? extends BaseUnit>)newValue);
				return;
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				setGameSystem((GameSystem)newValue);
				return;
			case WarmachineSystemPackage.ARMY_LIST__NAME:
				setName((String)newValue);
				return;
			case WarmachineSystemPackage.ARMY_LIST__CODE:
				setCode((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				getBaseUnits().clear();
				return;
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				setGameSystem((GameSystem)null);
				return;
			case WarmachineSystemPackage.ARMY_LIST__NAME:
				setName(NAME_EDEFAULT);
				return;
			case WarmachineSystemPackage.ARMY_LIST__CODE:
				setCode(CODE_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WarmachineSystemPackage.ARMY_LIST__BASE_UNITS:
				return baseUnits != null && !baseUnits.isEmpty();
			case WarmachineSystemPackage.ARMY_LIST__GAME_SYSTEM:
				return getGameSystem() != null;
			case WarmachineSystemPackage.ARMY_LIST__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case WarmachineSystemPackage.ARMY_LIST__CODE:
				return CODE_EDEFAULT == null ? code != null : !CODE_EDEFAULT.equals(code);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", code: ");
		result.append(code);
		result.append(')');
		return result.toString();
	}

} //ArmyListImpl
